/**
 * 客户资源录入脚本支持
 */
var key = null;
var modifydata = null;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
	init_editors();

	// 初始化选择
	if( !empty( modifydata ) )
	{
		setInitData( modifydata );
	}
} );

//--------------------------------------------------------------------------
//
//	consultant_client.php
//
//--------------------------------------------------------------------------

var _editor = [];

function button_bind()
{
	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	$( "a#btnAddItemGroup" ).click( add_item_group );
}

function del_item_group( e )
{
	if( confirm( "确定移除该选项组吗？" ) )
	{
		$( this ).parent().parent().remove();
		if( $( "#items div" ).length == 0 )
			$( "#items" ).prepend( "<span class='caption'>请点击左侧“+”添加选项组。</span>" );
	}
}

function show_hide_list( e )
{
	var ul = $( this ).parent().parent().find( "ul" );
	if( ul.visible() )
	{
		ul.hide();
	}
	else
	{
		ul.show();
	}
}

function set_item_group( g, d )
{
	if( empty( d ) )
		return;

	g.find( "input[name='topic']" ).val( d.topic );
	g.find( "ul" ).empty();

	var size = d.items.length;

	for( var i = 0; i < size; i++ )
	{
		add_item( g, d.items[ i ].q, d.items[ i ].t, d.items[ i ].i );
	}
}

function add_item_group( e, id )
{
	$( "#items > span" ).remove();

	var size = $( "#items .item_group" ).length;

	if( size == 19 )
	{
		infopad.err( "选项组最多添加 19 个。" );
		return;
	}

	id = empty( id ) ? hash( 12 ) : id;
	var h = "<div id=\"" + id + "\" class=\"item_group\" style=\"background-image: url(../img/num-"
			+ ( size + 1 ) + ".png);\">";

	h += "<p>";
	h += "<label>分组名称：</label>";
	h += "<input type=\"text\" name=\"topic\" size=\"50\" />";
	h += "<a rel=\"btnDelItemGroup\" class=\"del\" title=\"点击删除该选项组\"></a>";
	h += "<a rel=\"btnAddItem\" class=\"add\" title=\"点击添加新的选项\"></a>";
	h += "<a rel=\"btnItemShow\" class=\"search\" title=\"显示/隐藏选项列表\"></a>";
	h += "<a rel=\"btnItemExpand\" class=\"arrow\" title=\"展开此选项组并收起其它组\"></a>";
	h += "<a rel=\"btnItemUp\" class=\"arrow-up\" title=\"向上\"></a>";
	h += "<a rel=\"btnItemDown\" class=\"arrow-down\" title=\"向下\"></a>";
	h += "</p>";
	h += "<ul></ul>";
	h += "</div>";

	if( size > 0 )
	{
		$( "#items .item_group:last" ).after( h );
	}
	else
	{
		$( "#items" ).prepend( h );
	}

	var group = $( "#items .item_group:last" );
	bind_item_group_event( group );
	add_item( group );

	return group;
}

function bind_item_group_event( g )
{
	g.find( "a[rel='btnDelItemGroup']" ).click( del_item_group );
	g.find( "a[rel='btnAddItem']" ).click( function()
	{
		add_item( g );
	} );
	g.find( "a[rel='btnItemShow']" ).click( show_hide_list );

	g.find( "a[rel='btnItemExpand']" ).click( {
		self: g
	}, function( e )
	{
		$( "#items div.item_group" ).each( function( id )
		{
			var div = $( this );
			var ul = div.find( "ul" );

			if( div.attr( "id" ) == e.data.self.attr( "id" ) )
			{
				ul.show();
			}
			else
			{
				ul.hide();
			}
		} );
	} );

	g.find( "a[rel='btnItemUp']" ).click( item_group_up );
	g.find( "a[rel='btnItemDown']" ).click( item_group_down );

}

function item_group_up()
{
	var g = $( this ).parent().parent();
	var g2 = null;
	var index = g.index();

	if( index > 0 )
	{
		g2 = $( "td#items div.item_group" ).eq( index - 1 );
		g2.before( g );
	}
}

function item_group_down()
{
	var g = $( this ).parent().parent();
	var g2 = null;
	var index = g.index();
	var size = $( "td#items div.item_group" ).length;

	if( index < size - 1 )
	{
		g2 = $( "td#items div.item_group" ).eq( index + 1 );
		g2.after( g );
	}
}

function add_item( g, q, t, i )
{
	var h = "<li style='list-style:decimal inside;'>";

	h += "<input type=\"text\" name=\"question\" size=\"50\" />";
	h += "<select name=\"selector\">";
	h += "<option value=\"0\">单选</option>";
	h += "<option value=\"1\">多选</option>";
	h += "<option value=\"2\">输入</option>";
	h += "</select>";
	h += "<a rel=\"btnDelItem\" class=\"del\" title=\"点击删除该选项\"></a>";
	h += "<p rel=\"itemContainer\">";
	h += "<label>选项：</label>";
	h += "<input type=\"text\" name=\"items\" size=\"56\" placeholder=\"选项1,选项2,选项3,选项4,自定义\" />";
	h += "</p>";
	h += "</li>";

	g.find( "ul" ).append( h );

	// 尝试设置初始数据
	q = empty( q ) ? "" : q;
	t = empty( t ) ? 0 : t;
	i = empty( i ) ? "" : i;

	var li = g.find( "ul li:last" );
	li.find( "input[name='question']" ).val( q );
	li.find( "input[name='items']" ).val( i );
	li.find( "select[name='selector']" ).val( t );

	if( t == 2 )
		li.find( "p[rel='itemContainer']" ).hide();

	li.find( "select[name='selector']" ).change( {
		tar: li
	}, type_changed );

	li.find( "a[rel='btnDelItem']" ).click( function()
	{
		$( this ).parent().remove();
	} );

	li.find( "input[name='items']" ).bind( {
		focusin: format_items_symbol,
		focusout: format_items_symbol
	} );
}

function type_changed( e )
{
	var li = e.data.tar;

	// 当前选择
	var val = $( this ).val();

	if( val != 2 )
		li.find( "p[rel='itemContainer']" ).show();
	else
		li.find( "p[rel='itemContainer']" ).hide();
}

function stress_text( e )
{
	var hbox = $( this ).parent().find( "div[rel='result']" );
	var wrap = hbox.find( ">span[rel='STRESS']" );
	var html = hbox.html();

	if( wrap.length > 0 )
	{
		hbox.html( wrap.html() );
	}
	else
	{
		hbox.html( "<span rel=\"STRESS\" style=\"color:#ff0000;\">" + html + "</span>" );
	}
}

function format_items_symbol( e )
{
	var val = $( this ).val();

	val = $.trim( val );
	val = val.replace( /，/ig, "," );
	val = val.replace( /：/ig, ":" );
	val = val.replace( /\s*,\s*/ig, "," );
	val = val.replace( /\s*:\s*/ig, ":" );

	$( this ).val( val );
}

function init_editors()
{
	_editor = [];

	$( "div[contenteditable='true']" ).each( function()
	{
		_editor.push( $( this ).attr( "id" ) );
	} );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function setInitData( d )
{
	if( !empty( d ) )
	{
		key = d.key;

		$( "#topic" ).val( d.topic );
		$( "#desc" ).html( d.desc );

		// 通用定义
		var id = null;
		var group = null;
		var val = null;
		var ns = null;

		// 设置选项组
		for( ns in d.item )
		{
			// 设置分组数据
			val = d.item[ ns ];
			id = val.id;
			group = add_item_group( null, id );
			set_item_group( group, val );
		}

		// 收起所有选项组和评分组
		$( "td#items div.item_group ul" ).hide();
	}
	else
	{
		key = null;

		$( "#topic" ).val( "" );
		$( "#desc" ).html( "" );

		$( "td#items" ).find( ">span" ).remove();
	}
}

function submit_form()
{
	var data = {};

	// 待更新的数据
	data.key = key;

	// 分析问卷选项数据
	var item_groups = {};

	$( "#items div.item_group" ).each( function( id )
	{
		var it = parse_item_group( $( this ) );
		item_groups[ it.id ] = it;
	} );

	// 组织提交数据
	data.topic = $( "#topic" ).val();
	data.desc = htmlfilter( $( "#desc" ).html() );
	data.item = JSON.stringify( item_groups );

	$.post( "vote_add_go.php", data, function( value )
	{
		var d = $.parseJSON( value );

		if( d.err == 0 )
		{
			infopad.msg( d.msg );
			key = d.key;
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	} );
}

function parse_item_group( g )
{
	var items = [];
	var id = g.attr( "id" );
	var topic = g.find( "input[name='topic']" ).val();

	g.find( "ul li" ).each( function( id )
	{
		var q = $( this ).find( "input[name='question']" ).val();
		var i = $( this ).find( "input[name='items']" ).val();
		var t = $( this ).find( "select[name='selector']" ).val();
		var r = {
			q: q,
			t: t
		};

		if( t != 2 )
			r.i = i;

		items.push( r );
	} );
	return {
		id: id,
		topic: topic,
		items: items
	};
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
	else if( e.ctrlKey && e.shiftKey && e.which == 70 )// ctrl+shift+f
	{
		var edt = $( document.activeElement );
		var id = edt.attr( "id" );

		if( _editor.indexOf( id ) > -1 )
		{
			edt.html( htmlfilter( edt.html() ) );
		}
	}
}
